﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;
using System.Web;

namespace DataExportImporter
{
    public class Import
    {

        public string Username;
        public string Password;
        public string DomainName;
        public string WebPath;
        public string SubSite;
        public string ListName;

        public Import()
        {
            
        }



        public bool AddListItem(string xmlData, string queryXml)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(queryXml);
            System.Xml.XmlNode listViewFields = doc.SelectSingleNode("//ViewFields");


            string[] fieldNames = new string[listViewFields.ChildNodes.Count];


            int i = 0;
            foreach (System.Xml.XmlNode childNode in listViewFields.ChildNodes)
            {
                string FieldName = childNode.Attributes["Name"].Value;
                string prefixed_FieldName = "ows_" + FieldName;
                fieldNames[i] = prefixed_FieldName;
                i++;
            }


            //Get Parameters

            System.Xml.XmlNode ServerDetails = doc.SelectSingleNode("//Server");

            Username = XmlHelper.GetValue(ServerDetails, "Username");
            Password = XmlHelper.GetValue(ServerDetails, "Password");
            DomainName = XmlHelper.GetValue(ServerDetails, "DomainName");
            WebPath = XmlHelper.GetValue(ServerDetails, "Url");
            SubSite = XmlHelper.GetValue(ServerDetails, "SubSite");
            ListName = XmlHelper.GetValue(ServerDetails, "ListName");





            //Add Item
            listsWebService.Lists list = new DataExportImporter.listsWebService.Lists();
            list.Url = WebPath + SubSite + "/_vti_bin/lists.asmx";

            list.UseDefaultCredentials = false;
            list.Credentials = new NetworkCredential(Username, Password, DomainName);



            System.Xml.XmlDocument batchXml = new System.Xml.XmlDocument();

            XmlElement batch = doc.CreateElement("Batch");  

            batch.SetAttribute("OnError", "Continue");  
            batch.SetAttribute("ListVersion", "1");

            string Heading = @"<Method ID='1' Cmd='New'>";

          
            XmlDocument importedDataDoc = new XmlDocument();
            importedDataDoc.LoadXml(xmlData);


            foreach (System.Xml.XmlNode row in importedDataDoc.ChildNodes[0].ChildNodes)
            {
                StringBuilder batchPacket = new StringBuilder();
                batchPacket.Append(Heading);
               
                batchPacket.Append(CreateBatchItem(row, fieldNames));
                batchPacket.Append("</Method>");
                batch.InnerXml = batchPacket.ToString();

                
                list.UpdateListItems(ListName, batch);

            }



            

         return true;
        }



        public string CreateBatchItem(System.Xml.XmlNode row, string[] fieldNames)
        {

            StringBuilder XmlBatch = new StringBuilder();

            foreach(string fieldName in  fieldNames)
            {

               string KeyName = fieldName.Remove(0,4);

                string fieldValue = string.Empty;

                if (row.Attributes[fieldName] != null)
                {
                    fieldValue = row.Attributes[fieldName].Value;
                    string field = string.Format("<Field Name='{0}'>{1}</Field>", KeyName, HttpUtility.HtmlAttributeEncode(fieldValue));

                    XmlBatch.Append(field);
                }


                
            }


            return XmlBatch.ToString();
        }


    }
}
